#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

class B
{
public:
    B()
    {
        cout << "default constructor" << " ";
    }
    ~B()
    {
        cout << "destructed" << " ";
    }
    B(int i): data(i)
    {
        cout << "constructed by parameter" << data << " ";
    }
private:
    int data;
};
B Play( B b)
{
    return b;
}

void test1(){
    B temp = Play(5);
}

class parent{
    int i;
protected:
    int x;
public:
    parent(){
        x = 0;
        i = 0;
    }
    void change(){
        x++;
        i++;
    }
    void display();
};

class son:public parent{
public:
    void modify();
};

void parent::display() {
    cout << "x=" << x << endl;
}

void son::modify() {
    x++;
}

void test2(){
    son A;
    parent B;
    A.display();
    A.change();
    A.modify();
    A.display();
    B.change();
    B.display();
}

void Test1 ()
{
    string str, temp;
    vector<string> result;
    getline(cin, str);
    bool flag = true;
    int count = 0;
    for(int i = 0; i <= str.size(); i++) {
        //如果是空格或是最后一个字符，并且不是“”内的空格就保存起来
        if((str[i] == ' ' || i == str.size() ) && flag) {
            result.push_back(temp);
            temp.clear();
        }
        else if(str[i] != ' ' && str[i] != '\"')  // 不是空格，且不是“，就拼接起来
            temp += str[i];
        else if(str[i] == ' ' && !flag)// 是双引号内的空格也拼接起来
            temp += str[i];
        else if(str[i] == '\"') {
            count += 1;
            if(count % 2 == 1) { //遇到第一个双引号就不输出空格
                flag = false;
            }else if(count % 2 == 0) { // 第二个可以
                flag = true;
            }
        }
    }
    cout << result.size() << endl;
    for(auto & it : result)
        cout << it << endl;
}

void test3(){
    char *stop;
    char buffer[] = "2023";
    cout << strtol(buffer,&stop,16) << endl;
    char aim[100];
    _itoa(8227,aim,16);
    cout << aim << endl;
    printf("%c %d\n",'L','L');
    int x = 20202020;
    stringstream ss;
    ss << x;
    string str = ss.str();
    size_t a = str.find('2');
    cout << str << endl;
    cout << a;

}

#include <iostream>
#include<cmath>
#include<vector>
#include<climits>
using namespace std;

void getDivNum(vector<int>& a, int x) {
    for (int i = 2; i <= sqrt(x); ++i) {
        if (x % i == 0) {
            a.push_back(i);
            if (x / i != i)
                a.push_back(x / i);
        }
    }
}

int Jump(int n, int m) {
    vector<int> step(m + 1, INT_MAX);
    step[n] = 0;
    for (int i = n; i < m; ++i) {
        if (step[i] == INT_MAX)
            continue;
        vector<int> a;
        getDivNum(a, i);
        for (int j = 0; j < a.size(); j++) {
            if(i + a[j] <= m && step[i + a[j]] != INT_MAX) {
                //新的和旧的比较谁的步数少
                step[i + a[j]] = step[i + a[j]] > step[i] + 1 ? step[i] + 1 :step[i + a[j]];
            } else if (a[j] + i <= m) {
                step[a[j] + i] = step[i] + 1;
            }
        }
    }
    return step[m] == INT_MAX ? -1 : step[m];
}

int main() {
    int n, m;
    int min_step = 0;
    while (cin >> n >> m) {
        min_step = Jump(n, m);
        cout << min_step << endl;
    }
    return 0;
}


